package com.rpay.service.news.impl;

import com.rpay.common.dto.news.NewsDTO;
import com.rpay.common.dto.news.SqlQueryDTO;
import com.rpay.dao.news.NewsDao;
import com.rpay.model.news.NewsDO;
import com.rpay.model.news.SqlQueryDO;
import com.rpay.service.news.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.*;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsDao newsDao;

    @Override
    public SqlQueryDTO getDataList(SqlQueryDTO sqlQueryDTO, Integer page, Integer pageSize) {
        SqlQueryDO sqlQueryDO = convertSqlDTOToDO(sqlQueryDTO);
        List<NewsDO> newsDOList = newsDao.selectDataList(sqlQueryDO);
        List<NewsDTO> list = new ArrayList<>();
        if (newsDOList != null && newsDOList.size() > 0) {
            for (NewsDO newsDO : newsDOList) {
                NewsDTO newsDTO = convertDOToDTO(newsDO);
                list.add(newsDTO);
            }
        }
        Integer count = newsDao.selectCount(sqlQueryDO);
        SqlQueryDTO data = convertSqlDOToDTO(list, page, pageSize, count);
        return data;
    }

    @Override
    public boolean saveNews(NewsDTO newsDTO) {
        NewsDO newsDO = convertDTOToDO(newsDTO);
        newsDao.insert(newsDO);
        return true;
    }

    @Override
    public NewsDTO getDetail(NewsDTO newsDTO) {
        NewsDO query = convertDTOToDO(newsDTO);
        NewsDO newsDo = newsDao.findById(query);
        NewsDTO detail = convertDOToDTO(newsDo);
        return detail;
    }

    @Override
    public boolean deleteNews(Integer id) {
        newsDao.deleteById(id);
        return true;
    }

    @Override
    public boolean updateNews(NewsDTO newsDTO) {
        NewsDO newsDO = convertDTOToDO(newsDTO);
        newsDao.updateById(newsDO);
        return true;
    }

    private NewsDTO convertDOToDTO(NewsDO newsDO) {
        NewsDTO newsDTO = null;
        if (newsDO != null) {
            newsDTO = new NewsDTO();
            newsDTO.setId(newsDO.getId());
            newsDTO.setVisible(newsDO.getVisible());
            newsDTO.setNewsType(newsDO.getNewsType());
            newsDTO.setTitle(newsDO.getTitle());
            newsDTO.setEnTitle(newsDO.getEnTitle());
            newsDTO.setSubtitle(newsDO.getSubtitle());
            newsDTO.setEnSubtitle(newsDO.getEnSubtitle());
            newsDTO.setSource(newsDO.getSource());
            newsDTO.setEnSource(newsDO.getEnSource());
            newsDTO.setCover(newsDO.getCover());
            newsDTO.setEnCover(newsDO.getEnCover());
            newsDTO.setContent(newsDO.getContent());
            newsDTO.setEnContent(newsDO.getEnContent());
            newsDTO.setCreateTime(newsDO.getCreateTime());
            newsDTO.setUpdateTime(newsDO.getUpdateTime());
        }
        return newsDTO;
    }

    private NewsDO convertDTOToDO(NewsDTO newsDTO) {
        NewsDO newsDO = null;
        if (newsDTO != null) {
            newsDO = new NewsDO();
            newsDO.setId(newsDTO.getId());
            newsDO.setVisible(newsDTO.getVisible());
            newsDO.setTitle(newsDTO.getTitle());
            newsDO.setEnTitle(newsDTO.getEnTitle());
            newsDO.setContent(newsDTO.getContent());
            newsDO.setEnContent(newsDTO.getEnContent());
            newsDO.setNewsType(newsDTO.getNewsType());
            newsDO.setSource(newsDTO.getSource());
            newsDO.setEnSource(newsDTO.getEnSource());
            newsDO.setSubtitle(newsDTO.getSubtitle());
            newsDO.setEnSubtitle(newsDTO.getEnSubtitle());
            newsDO.setCover(newsDTO.getCover());
            newsDO.setEnCover(newsDTO.getEnCover());
        }
        return newsDO;
    }

    private SqlQueryDO convertSqlDTOToDO(SqlQueryDTO sqlQueryDTO) {
        SqlQueryDO sqlQueryDO = null;
        if (sqlQueryDTO != null) {
            sqlQueryDO = new SqlQueryDO();
            sqlQueryDO.setVisible(sqlQueryDTO.getVisible());
            sqlQueryDO.setNewsType(sqlQueryDTO.getNewsType());
            sqlQueryDO.setKeyword(sqlQueryDTO.getKeyword());
            sqlQueryDO.setPage(sqlQueryDTO.getPage());
            sqlQueryDO.setPageSize(sqlQueryDTO.getPageSize());
        }
        return sqlQueryDO;
    }

    private SqlQueryDTO convertSqlDOToDTO(List list, Integer page, Integer pageSize, Integer count) {
        SqlQueryDTO sqlQueryDTO = null;
        sqlQueryDTO = new SqlQueryDTO();
        sqlQueryDTO.setList(list);
        sqlQueryDTO.setPage(page);
        sqlQueryDTO.setPageSize(pageSize);
        sqlQueryDTO.setTotalRow(count);
        return sqlQueryDTO;
    }
}
